package dataserver.controller;

import com.github.wenhao.jpa.Specifications;
import dataserver.dto.ScenePointDTO;
import dataserver.pojo.SceneDevice;
import dataserver.pojo.ScenePoint;
import dataserver.service.ScenePointService;
import dataserver.utils.JsonUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author lrz
 */
@Controller
@CrossOrigin
@RequestMapping("/point")
@Tag(name = "标记点管理")
public class ScenePointController extends BaseController {
    private static final Logger logger = LogManager.getLogger(ScenePointController.class);

    @Autowired
    ScenePointService scenePointService;

    /**
     * 保存数据
     */
    @Operation(summary = "保存数据")
    @PostMapping()
    public void save(ScenePointDTO scenePointDTO, HttpServletResponse response) throws Exception {
        try {
            if (scenePointDTO != null) {
                // 将dto转为vo
                ScenePoint sceneFence = (ScenePoint)JsonUtil.getJsonToBean(scenePointDTO, ScenePoint.class);
                sceneFence.setCreateTime(new Date());
              int t=  scenePointService.save(sceneFence);
                returnCollection(response, 1);
            } else {
                returnCollection(response, 0);
            }
        } catch (Exception e) {
            returnCollection(response, e);
        }
    }

    /**
     * 根据ID删除
     */
    @Operation(summary = "根据ID删除")
    @DeleteMapping(value = "/{id}")
    public void deleteById(@PathVariable String id, HttpServletResponse response) throws Exception {
        try {
            scenePointService.deleteById(id);
            returnCollection(response, 1);
        } catch (Exception e) {
            returnCollection(response, e);
        }
    }


    /**
     * 分页查询
     */
    @Operation(summary = "根据条件分页查询信息")
    @GetMapping(value = "")
    public void findPageData(HttpServletResponse response,
                             @RequestParam(required = false) String stationId,
                             @RequestParam(required = false) String stationName,
                             @RequestParam(required = false) String name,
                             @RequestParam(required = false, defaultValue = "1") int page,
                             @RequestParam(required = false, defaultValue = "10") int size) throws Exception {
        try {
            // 创建匹配器，需要查询条件请修改此处代码
            Specification<ScenePoint> specification = Specifications.<ScenePoint>and()
                    .eq(!StringUtils.isBlank(stationId), "stationId", stationId)
                    .eq(!StringUtils.isBlank(stationName), "stationName", stationName)
                    .eq(!StringUtils.isBlank(name), "name", name)
                    .build();
            Pageable pageable = PageRequest.of(page - 1, size, Sort.Direction.DESC, "stationId");
            Page<ScenePoint> pageData = scenePointService.findPageData(specification, pageable);
            String targetData = getPageData(pageData);
            returnCollection(response, targetData);
        } catch (Exception e) {
            returnCollection(response, e);
        }
    }

    // 根据条件查询信息
    @Operation(summary = "根据条件查询信息")
    @GetMapping(value = "/findData")
    public void findData(HttpServletResponse response,
                         @RequestParam(required = false) String stationId,
                         @RequestParam(required = false) String stationName,
                         @RequestParam(required = false) String name) throws Exception {
        try {
            // 创建匹配器，需要查询条件请修改此处代码
            Specification<ScenePoint> specification = Specifications.<ScenePoint>and()
                    .eq(!StringUtils.isBlank(stationId), "stationId", stationId)
                    .eq(!StringUtils.isBlank(stationName), "stationName", stationName)
                    .eq(!StringUtils.isBlank(name), "name", name)
                    .build();
            List<ScenePoint> sourceData = scenePointService.findData(specification);
            returnCollection(response, sourceData);
        } catch (Exception e) {
            returnCollection(response, e);
        }
    }

}
